package es.uji.viselab.benchmark;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import es.uji.viselab.benchmark.DataPackageType;
import es.uji.viselab.benchmark.database.BMDataType;

public class DataPackage {
	private DataPackageType dataType;
	private int sequence;
	private List<Double> dataList;

	public DataPackage() {
		dataList = new ArrayList<Double>();
		dataType = null;
		setSequence(1);
	}

	public DataPackage(List<String> list) {

		// call the default constructor
		this();

		Iterator<String> iterator = list.iterator();
		double dblVal = 0;
		String strVal;

		// First value is the type of the data: it can be a position, velocity,
		// error distance, etc.
		String dataTypeString = iterator.next();
		dataType=DataPackageType.valueOf(dataTypeString);
		sequence = (int) Double.parseDouble(iterator.next());
		// Next values should always be double

		// DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols();
		// otherSymbols.setDecimalSeparator('.');
		// otherSymbols.setGroupingSeparator(',');
		// DecimalFormat df = new DecimalFormat("#######.#####", otherSymbols);

		while (iterator.hasNext()) {
			strVal = iterator.next();
			// dblVal = df.parse(strVal).doubleValue();
			dblVal = Double.parseDouble(strVal);
			dataList.add(dblVal / 1000000);
		}

	}

	public DataPackage(String str, int seq, int x, int y, int z) {
		this();
		setType(str);
		setSequence(seq);
		addValue(x);
		addValue(y);
		addValue(z);

	}

	public DataPackage(String str, int i) {
		this();
		setType(str);
		setSequence(i);
	}

	public DataPackage(DataPackageType dataType, double x, double y, double z) {
		this();
		setType(dataType);
		addValue(x);
		addValue(y);
		addValue(z);

	}

	private void setType(DataPackageType dt) {
		setType(dt.toString());
		
	}

	public void addValue(double d) {
		dataList.add(d);
	}

	public void setType(String str) {
		dataType = DataPackageType.valueOf(str);
	}

	public void setValue(double val) {
		dataList.add(val);
	}

	public DataPackageType getType() {
		return dataType;
	}

	public String print() {
		String s = new String(dataType.toString()) + ": ";
		Iterator<Double> iterator = dataList.iterator();
		while (iterator.hasNext())
			s = s + iterator.next() + " ";
		return s;
	}

	public int getSequence() {
		return sequence;
	}

	public void setSequence(int sequence) {
		this.sequence = sequence;
	}

	public double getValue(int i) {

		double val = 0;
		try {
			val = dataList.get(i - 1);
		} catch (java.lang.IndexOutOfBoundsException e) {

		}
		return val;
	}

	/*
	 * public List<String> toStringList() { List<String> list = new
	 * ArrayList<String>(); list.add(dataType);
	 * list.add(Integer.toString(sequence)); Iterator<Double> iterator =
	 * dataList.iterator(); while (iterator.hasNext())
	 * list.add(iterator.next().toString()); return list; }
	 */
	/*public void insert() {
		BMDatabaseManager.getInstance().getConnection().insert(this);
	}*/

	public int getLenght() {
		return dataList.size();
	}

	public void clean() {
		dataList = new ArrayList<Double>();
		dataType = null;
		setSequence(1);
	}

	public boolean equalsType(BMDataType dt) {
		return dt.ordinal()==this.dataType.ordinal();
	}

}
